package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.GasModel;
import com.aysafety.iotdataplatformserver.dao.GasDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 瓦斯
@Component public class GasListener {

    private final String type = "Gas";

    private final String tableName = "GasRealTime";

    private final String calTableName = "CalGas";

    private final String[] structure = new String[] {"Gas"};

    private final String[] gasEmissionStructure = new String[] {"gasEmission"};

    private final String calWarningTableName = "CalWarningGas";

    private final String[] calWarningStructure = new String[] {"warning"};
    @Autowired KsTreeDataListener ksTreeDataListener;
    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);
    @Autowired private GasDao gasDao;

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public void createCalTable() throws IOException {
        HBaseDriver.createTable(this.calTableName, this.gasEmissionStructure);
    }

    public void createCalWarningTable() throws IOException {
        HBaseDriver.createTable(this.calWarningTableName, this.calWarningStructure);
    }

    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    GasModel gas = this.transformData(listData);
                    this.save(gas, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    GasModel gas = this.transformData(listData);
                    this.save(gas, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("GasListener:", e);
        }
    }

    public boolean save(GasModel gas, String clientId) throws IOException {
        String timestamp = String.valueOf(gas.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", gas.getEntId() + "." + "gasrealtime");
        map.put("pk", "\'" + timestamp+".:." + gas.getDeviceId() + "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + gas.getDeviceId() + "\'");
        map.put("entId", "\'" + gas.getEntId() + "\'");
        map.put("unit", "\'" + gas.getUnit() + "\'");
        map.put("workingFace", "\'" + gas.getWorkingFace() + "\'");
        map.put("datetime", gas.getDatetime().getTime());
        map.put("monitoringValue", gas.getMonitoringValue());
        map.put("poklad", gas.getPoklad());
        map.put("smallestRange", gas.getSmallestRange());
        map.put("maximumRange", gas.getMaximumRange());
        map.put("stationName", "\'" + gas.getStationName() + "\'");
        map.put("status", gas.isStatus());
        map.put("powerCutArea", "\'" + gas.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", gas.getPowerReconnectionLimit());
        map.put("powerCutLimit", gas.getPowerCutLimit());
        gasDao.insertRealGas(map);
        ksTreeDataListener
            .generateTreeData(gas.getEntId(), this.type, gas.getWorkingFace(), gas.getDeviceId(), gas.getDatetime(),
                "");
        return true;
    }

    public GasModel transformData(Map<String, Object> result) {
        GasModel gas = new GasModel();
        if (null != result.get("entId")) {
            gas.setEntId(result.get("entId").toString());
        } else {
            gas.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            gas.setDeviceId(result.get("deviceId").toString());
        } else {
            gas.setDeviceId("无数据");
        }
        gas.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            gas.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            gas.setPoklad(-1.0);
        }
        gas.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            gas.setWorkingFace(result.get("workingFace").toString());
        } else {
            gas.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            gas.setUnit(result.get("unit").toString());
        } else {
            gas.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            gas.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            gas.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            gas.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            gas.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            gas.setStationName(result.get("stationName").toString());
        } else {
            gas.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                gas.setStatus(TRUE);
            } else {
                gas.setStatus(FALSE);
            }
        } else {
            gas.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            gas.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            gas.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            gas.setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            gas.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            gas.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            gas.setPowerCutLimit(-1.0);
        }
        return gas;
    }
}
